/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddMethodDlg.java
 *
 * Created on 2010-9-30, 11:08:42
 */
package dataanalysis.gui.dialog;

import dataanalysis.MyUtils;
import dataanalysis.main.MainView;
import java.io.File;
import java.util.Vector;

/**
 *
 * @author Administrator
 */
public class AddMethodDlg extends javax.swing.JDialog {

    private final MainView m_manager;
    private final Vector<File> m_fileVector = new Vector();
    private final Vector<String> m_stringVector = new Vector();

    /** Creates new form AddMethodDlg */
    public AddMethodDlg(java.awt.Frame parent, boolean modal, MainView manager) {
        super(parent, modal);
        m_manager = manager;
        initComponents();
        this.setTitle("添加方法");
        this.setLocationRelativeTo(parent);
        this.addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                exit();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_packetNameTxt = new javax.swing.JTextField();
        m_methodNameTxt = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        m_typeComb = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);

        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("包名称:");

        jLabel2.setText("方法名称:");

        jLabel3.setText("文件列表:");

        jButton1.setText("添 加");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("删 除");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("取 消");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("确 定");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel4.setText("方法类型:");

        m_typeComb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "weka.filters.Filter", "weka.attributeSelection.ASEvaluation", "weka.attributeSelection.ASSearch", "weka.attributeSelection.semiAS.SemiAttributeSelector", "weka.classifiers.Classifier", "weka.clusterers.Clusterer" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(m_typeComb, 0, 311, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel1))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(m_methodNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                                            .addComponent(m_packetNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton1)
                            .addComponent(jButton3)
                            .addComponent(jButton4)))
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_packetNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_methodNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(m_typeComb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton4)
                        .addGap(8, 8, 8))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        m_manager.addMethodAction();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.deleteFile();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        String classPath = System.getProperty("user.dir") + "\\build\\classes";
        String srcPath=System.getProperty("user.dir") + "\\src";
        String clsGCPath = classPath + "\\weka\\gui\\GenericPropertiesCreator.props";
        String clsGEPath = classPath + "\\weka\\gui\\GenericObjectEditor.props";
        String srcGCPath = srcPath + "\\weka\\gui\\GenericPropertiesCreator.props";
        String srcGEPath = srcPath + "\\weka\\gui\\GenericObjectEditor.props";
        String type = m_typeComb.getSelectedItem().toString();
        String packet = type.substring(0, type.lastIndexOf('.')) + "." + m_packetNameTxt.getText().trim();
        String method = packet + "." + m_methodNameTxt.getText().trim();
        try {
            MyUtils.insertToFile(clsGCPath, type, " " + packet + ",\\");
            MyUtils.insertToFile(clsGEPath, type, " " + method + ",\\");
            MyUtils.insertToFile(srcGCPath, type, " " + packet + ",\\");
            MyUtils.insertToFile(srcGEPath, type, " " + method + ",\\");
            for (int i = 0; i < m_fileVector.size(); i++) {
                File file = m_fileVector.get(i);
                String oldPath = file.getAbsolutePath();
                String newPath = classPath + "\\" + packet.replace('.', '\\') + "\\" + file.getName();
                MyUtils.copyFile(oldPath, newPath);

            }
            switch (m_typeComb.getSelectedIndex()) {
                case 0:
                    m_manager.RebuildPreEditor();
                    break;
                case 1:
                    m_manager.RebuildEvlEditor();
                    break;
                case 2:
                    m_manager.RebuildSchEditor();
                    break;
                case 3:
                    m_manager.RebuildSemiASEditor();
                    break;
                case 4:
                    m_manager.RebuildClsEditor();
                    break;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            m_manager.showErrorDlg(ex, "添加文件错误!");
        }
        this.exit();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.exit();
    }//GEN-LAST:event_jButton3ActionPerformed
//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AddMethodDlg dialog = new AddMethodDlg(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField m_methodNameTxt;
    private javax.swing.JTextField m_packetNameTxt;
    private javax.swing.JComboBox m_typeComb;
    // End of variables declaration//GEN-END:variables

    public void addFiles(File[] files) {
        for (int i = 0; i < files.length; i++) {
            m_fileVector.add(files[i]);
            m_stringVector.add(files[i].getAbsolutePath());
            this.updateList();
        }
    }

    private void deleteFile() {
        int i = jList1.getSelectedIndex();
        if (i != -1) {
            m_fileVector.remove(i);
            m_stringVector.remove(i);
            this.updateList();
        }
    }

    private void updateList() {
        jList1.setListData(m_stringVector);
        jList1.updateUI();
    }

    private void exit() {
        this.setVisible(false);
        m_fileVector.removeAllElements();
        m_stringVector.removeAllElements();
    }
}
